package fiszki.namespace;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;


public class EditSetActivity extends Activity {
	protected EditText name;
	protected ImageButton saveSet;
	protected ImageButton cancel;
    protected SetsDBAdapter setsDBAdapter;
    int setID;
    
    // OK
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().getAttributes().format = android.graphics.PixelFormat.RGBA_8888;
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.add_new_set);
        
        initUiElements();
        initButtonsOnClickListeners();
        openNewConnection();
	}
	
	// OK
	protected void initUiElements() {
		saveSet = (ImageButton) findViewById(R.id.buttonSaveSet);
		cancel = (ImageButton) findViewById(R.id.buttonCancel);
		name = (EditText) findViewById(R.id.name);
		setID = getIntent().getExtras().getInt("setID");
        
		String[] DayOfWeek = {"Chiński", "Inny chiński", "Sośny",
		  "Azjatycko-chiński"};

		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
		R.layout.spinner_item, R.id.spCountry, DayOfWeek );
		//ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
		//		  this, R.array.countries_array, R.layout.spinner_item );
		adapter.setDropDownViewResource( R.layout.spinner_item );
		Spinner chQuestLang = (Spinner) findViewById( R.id.questLang );
		Spinner chAnswLang = (Spinner) findViewById(R.id.answerLang);
		chQuestLang.setAdapter(adapter);		 
		chAnswLang.setAdapter(adapter);

        name.setText(getIntent().getExtras().getString("name"));
        chQuestLang.setSelection(adapter.getPosition(getIntent().getExtras().getString("qLang")));
        chAnswLang.setSelection(adapter.getPosition(getIntent().getExtras().getString("aLang")));
 	}
    
	protected void initButtonsOnClickListeners() { 	 	
    	saveSet.setOnClickListener(new View.OnClickListener() {
            
			@Override
			public void onClick(View v) {
            	try {
            		saveSet();
            		Intent i = new Intent(EditSetActivity.this, SetsActivity.class);
            		startActivity(i);
            		finish();
            	}
            	catch(Exception e) {       		
            	}
            }
        });
    	
    	cancel.setOnClickListener(new View.OnClickListener() {
            
			@Override
			public void onClick(View v) {
				Intent i = new Intent(EditSetActivity.this, SetsActivity.class);
    			startActivity(i);
            	finish();
            }
        });
	}
    
	protected void openNewConnection() {
        setsDBAdapter = new SetsDBAdapter(getApplicationContext());
        setsDBAdapter.open();
    }	
	
	protected void saveSet() throws Exception {
    	
        String setName = name.getText().toString(); 
 
        final Spinner qLangHandler = (Spinner) findViewById(R.id.questLang);  
        final Spinner aLangHandler = (Spinner) findViewById(R.id.answerLang);  

        String qLang = qLangHandler.getSelectedItem().toString();         
        String aLang = aLangHandler.getSelectedItem().toString();  
        
        if(setName.equals("")) {
        	name.setError("Pole 'Nazwa zestawu' nie moze byc puste.");
        	throw new Exception();
        }
        
        setsDBAdapter.updateSet(setID, setName, qLang, aLang);
    }
	
	// OK
    @Override
    protected void onDestroy() {
        if(setsDBAdapter != null)
            setsDBAdapter.close();
        
        super.onDestroy();
    }
}
